﻿#region

using System;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using log4net;
using xConnected.Common.Category;
using xConnected.Common.Country;
using xConnected.Common.ExpertProfile;
using xConnected.Infrastructure;
using xConnected.Service;

#endregion

namespace xConnected.Controllers
{
    public class HomeController : Controller
    {
        private readonly CategoryService _categoryService;
        private readonly CountryService _countryService;
        private readonly EducationService _educationService;
        private readonly LanguageService _languageService;
        private readonly CompanyProfileService _companyService;
        private readonly TechnologyService _technologyService;
        private readonly UserService _userService;
        private readonly IndustryService _industryService;


        public HomeController(ExpertProfileService profileService,
                                         EducationService educationService,
                                         CountryService countryService,
                                         TechnologyService technologyService,
                                         CategoryService categoryService,
                                         UserService userService,
                                         LanguageService languageService,
                                         IndustryService industryService)
        {
            _educationService = educationService;
            _countryService = countryService;
            _technologyService = technologyService;
            _categoryService = categoryService;
            _userService = userService;
            _languageService = languageService;
            _industryService = industryService;
        }


        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Activate(string activationKey)
        {
              ILog log = LogManager.GetLogger(GetType());
              try
              {
                  //var user = _userService.GetAll().First(u => u.ActivationKey == activationKey);
                  //user.IsActivated = true;
                  //_userService.SaveOrUpdate(user);
                  return Json("Ok", JsonRequestBehavior.AllowGet);
              }

              catch (Exception ex)
              {
                  log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                  return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
              }
        }

        public ActionResult Entities()
        {
            return View();
        }

        public ActionResult SetCulture(string id)
        {
            HttpCookie userCookie = Request.Cookies["Culture"];

            userCookie.Value = id;
            userCookie.Expires = DateTime.Now.AddYears(100);
            Response.SetCookie(userCookie);

            return Redirect(Request.UrlReferrer.ToString());
        }

        public JsonResult GetJsonIndustries()
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var industries = _industryService.GetAll().OrderBy(t => t.Description).Select(l => new
                                                                                                       {
                                                                                                           l.Id,
                                                                                                           l.Created,
                                                                                                           l.Updated,
                                                                                                           l.Description,
                                                                                                           l.Key
                                                                                                       });
                return Json(industries, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetJsonCompanies()
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var companies = _companyService.GetAll().ToList().Select(c => new
                                                                       {
                                                                           c.Id,
                                                                           c.Created,
                                                                           c.Updated,
                                                                           c.Title,
                                                                           c.Description,
                                                                           c.Location,
                                                                           c.TagLine,
                                                                           c.Website,
                                                                           Logo = c.Photo,
                                                                           Positions = c.Positions.Select(p => new
                                                                                                                 {
                                                                                                                     p.
                                                                                                                 Id,
                                                                                                                     p.
                                                                                                                 Created,
                                                                                                                     p.
                                                                                                                 Updated,
                                                                                                                     p.
                                                                                                                 Title,
                                                                                                                     p.
                                                                                                                 Description,
                                                                                                                     p.
                                                                                                                 StartDate,
                                                                                                                     p.
                                                                                                                 Duration,
                                                                                                                     p.
                                                                                                                 Location,
                                                                                                                     ContractType = p.
                                                                                                                 JobType,
                                                                                                                     Categories
                                                                                                                 =
                                                                                                                 p.
                                                                                                                 Categories
                                                                                                                 .Select
                                                                                                                 (
                                                                                                                     k
                                                                                                                     =>
                                                                                                                     new
                                                                                                                         {
                                                                                                                             k
                                                                                                                         .
                                                                                                                         Id,
                                                                                                                             k
                                                                                                                         .
                                                                                                                         Created,
                                                                                                                             k
                                                                                                                         .
                                                                                                                         Updated,
                                                                                                                             Description
                                                                                                                         =
                                                                                                                         CategoryRes
                                                                                                                         .
                                                                                                                         ResourceManager
                                                                                                                         .
                                                                                                                         GetString
                                                                                                                         (k
                                                                                                                              .
                                                                                                                              Key,
                                                                                                                          Thread
                                                                                                                              .
                                                                                                                              CurrentThread
                                                                                                                              .
                                                                                                                              CurrentCulture),
                                                                                                                             k
                                                                                                                         .
                                                                                                                         Key
                                                                                                                         }),
                                                                                                                     Technologies
                                                                                                                 =
                                                                                                                 p.
                                                                                                                 Technologies
                                                                                                                 .Select
                                                                                                                 (
                                                                                                                     t
                                                                                                                     =>
                                                                                                                     new
                                                                                                                         {
                                                                                                                             t
                                                                                                                         .
                                                                                                                         Id,
                                                                                                                             t
                                                                                                                         .
                                                                                                                         Created,
                                                                                                                             t
                                                                                                                         .
                                                                                                                         Updated,
                                                                                                                             t
                                                                                                                         .
                                                                                                                         Name
                                                                                                                         }),
                                                                                                                     Country
                                                                                                                 = new
                                                                                                                       {
                                                                                                                           c
                                                                                                                 .
                                                                                                                 Country
                                                                                                                 .Id,
                                                                                                                           c
                                                                                                                 .
                                                                                                                 Country
                                                                                                                 .
                                                                                                                 Created,
                                                                                                                           c
                                                                                                                 .
                                                                                                                 Country
                                                                                                                 .
                                                                                                                 Updated,
                                                                                                                           Description
                                                                                                                 =
                                                                                                                 CountryRes
                                                                                                                 .
                                                                                                                 ResourceManager
                                                                                                                 .
                                                                                                                 GetString
                                                                                                                 (c.
                                                                                                                      Country
                                                                                                                      .
                                                                                                                      Key,
                                                                                                                  Thread
                                                                                                                      .
                                                                                                                      CurrentThread
                                                                                                                      .
                                                                                                                      CurrentCulture),
                                                                                                                           c
                                                                                                                 .
                                                                                                                 Country
                                                                                                                 .
                                                                                                                 ShortName
                                                                                                                       }
                                                                                                                 }),
                                                                           Country = new
                                                                                         {
                                                                                             c.Country.Id,
                                                                                             c.Country.Created,
                                                                                             c.Country.Updated,
                                                                                             Description =
                                                                       CountryRes.ResourceManager.GetString(
                                                                           c.Country.Key,
                                                                           Thread.CurrentThread.CurrentCulture),
                                                                                             c.Country.ShortName
                                                                                         },
                                                                           c.CountryId
                                                                       });

                return Json(companies, JsonRequestBehavior.AllowGet);
            }

            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        

       
    }
}